#include <graph/shentu_graph.hpp>
#include <graph/graph.hpp>
#include <util/xvector.hpp>
#include <fstream>
#include <iostream>
#include <set>

using namespace std;

struct my_test_w{
    float a;
    float b;
    float c;
};

template <typename W> using coo_edge = weighted<unweighted_coo_edge, W>;
using edge_list = xvector<coo_edge<std::monostate>>;
using edge_list_float = xvector<coo_edge<float>>;
using edge_list_my_test_w = xvector<coo_edge<my_test_w>>;
using vertex_feature = double[14];
using vertex_label = int;

int read_csv_weighted(string fname, edge_list_float& edges){
	ifstream in_file(fname,ios::in);
	string line;
	set<uint64_t> vertex_set;
	// 读headers
	getline(in_file,line);
	while(getline(in_file,line)){
		string number;
		istringstream readstr(line);
		vector<int> data_line;
		// 读序号
		// getline(readstr,number,',');
		for(int i = 0; i < 3; i++){
			getline(readstr,number,',');
			data_line.push_back(atoi(number.c_str()));
		}
		edges.push_back({{data_line[0],data_line[1]},float{data_line[2]}});

		vertex_set.insert(data_line[0]);
		vertex_set.insert(data_line[1]);
	}
	in_file.close();
	return vertex_set.size();
}

void write_coo_float(string fname, edge_list_float& edges_float){
	ofstream out_file(fname.c_str(),ios::out | ios::binary);
	if(!out_file){
		printf("out file open error!\n");
		return ;
	}
	constexpr size_t coo_size = 2 * sizeof(uint64_t);//is_same_v不命中的话，coo_size就是存两个顶点vertex的容量
	out_file.write((char*)edges_float.begin(),edges_float.size() * sizeof(edges_float[0]));
	out_file.close();
}

int main(int arvc, char** argv){
	edge_list_float edges_float;

	string fname = argv[1];
	string my_data_csv_folder = "../data/csv_data/";
    string my_data_folder = "../data/binary_data/";

    string csv_fname{my_data_csv_folder+fname+".csv"};

    int nvertex = read_csv_weighted(csv_fname, edges_float);
    string binary_fname{my_data_folder+to_string(nvertex)+"-"+fname+".dat"};

	cout << binary_fname << endl;
    write_coo_float(binary_fname,edges_float);

    return 0;
}